package com.dernan.myhomelive;


import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.utils.Log;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

;


public class MainActivity extends Activity {

    //    private WebView webView;
    private VideoView mVideoView;
    private GestureDetector mGestureDetector;
    private int verticalMinistance = 20;            //水平最小识别距离
    private int minVelocity = 10;           //最小识别速度
    private String[] mUrlPath = {"rtmp://115.182.9.51/live/chaozhou", "rtmp://115.182.9.51/live/chaozhou1"};
    private int mIndex = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        //        webView = (WebView) findViewById(R.id.wv_oauth);
        //        webView.loadUrl(URL);
        //        webView.getSettings().setJavaScriptEnabled(true);

        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏

        if (!LibsChecker.checkVitamioLibs(this)) {
            System.out.print("failed");
            Log.i("Suc", "bad");
            return;
        }
        setContentView(R.layout.activity_main);
        mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);
        mVideoView.setVideoChroma(MediaPlayer.VIDEOCHROMA_RGB565);


        //        path = "rtmp://rrbalancer.broadcast.tneg.de:1935/pw/ruk/ruk";
            /*options = new HashMap<>();
            options.put("rtmp_playpath", "");
            options.put("rtmp_swfurl", "");
            options.put("rtmp_live", "1");
            options.put("rtmp_pageurl", "");*/


        //mVideoView.setVideoURI(Uri.parse(path), options);
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();
        mVideoView.setVideoPath(mUrlPath[1]);
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setPlaybackSpeed(1.0f);
                mVideoView.start();
                mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_STRETCH, 0);

            }
        });

        mGestureDetector = new GestureDetector(this, new MyOnGestureListener());


        Log.i("Suc", "good");

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector.onTouchEvent(event))
            return true;

        // 处理手势结束
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:

                break;
        }

        return super.onTouchEvent(event);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                choiceLastTv();
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                choiceNextTv();
                break;

            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onResume() {
        /**
         * 设置为横屏
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onResume();
    }

    private class MyOnGestureListener extends SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }
        //
        //        @Override
        //        public void onShowPress(MotionEvent motionEvent) {
        //
        //        }
        //
        //        @Override
        //        public boolean onSingleTapUp(MotionEvent motionEvent) {
        //
        //            return true;
        //        }
        //
        //
        //        /** 滑动 */
        //        @Override
        //        public boolean onScroll(MotionEvent e1, MotionEvent e2,
        //                                float distanceX, float distanceY) {
        //            float mOldX = e1.getX(), mOldY = e1.getY();
        //            int y = (int) e2.getRawY();
        //            Display disp = getWindowManager().getDefaultDisplay();
        //            int windowWidth = disp.getWidth();
        //            int windowHeight = disp.getHeight();
        //
        //            if (mOldX > windowWidth * 4.0 / 5)// 右边滑动
        //            {
        //                mIndex++;
        //                changeTVLive(mIndex);
        //            }else if (mOldX < windowWidth / 5.0)// 左边滑动
        //            {
        //                mIndex--;
        //                changeTVLive(mIndex);
        //            }
        //            return super.onScroll(e1, e2, distanceX, distanceY);
        //        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (mVideoView.isPlaying()) {
                mVideoView.pause();
            } else {
                mVideoView.start();
            }
            return super.onDoubleTap(e);
        }

        //
        //        @Override
        //        public void onLongPress(MotionEvent motionEvent) {
        //
        //        }
        //
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            //大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度，则执行相应方法
            if (e1.getX() - e2.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity) {
                choiceLastTv();

            } else if (e2.getX() - e1.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity) {
                choiceNextTv();

            } else if (e1.getY() - e2.getY() > 20 && Math.abs(velocityY) > 10) {
                //                Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT).show();

            } else if (e2.getY() - e1.getY() > 20 && Math.abs(velocityY) > 10) {
                //                Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT).show();
            }

            return false;
        }
    }

    private void choiceLastTv() {
        mIndex--;
        changeTVLive(mIndex);
        Toast.makeText(MainActivity.this, "跳转到上一频道", Toast.LENGTH_SHORT).show();
    }

    private void choiceNextTv() {
        mIndex++;
        changeTVLive(mIndex);
        Toast.makeText(MainActivity.this, "跳转到下一频道", Toast.LENGTH_SHORT).show();
    }

    private void changeTVLive(int index) {

        if (index < 0) {
            mIndex = mUrlPath.length - 1;
        } else if (index >= mUrlPath.length) {
            mIndex = 0;
        }
        mVideoView.setVideoPath(mUrlPath[mIndex]);
        mVideoView.start();
    }
}
